package zhoug.common.content;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import androidx.annotation.IntDef;

import static java.lang.annotation.RetentionPolicy.SOURCE;

/**
 * 文件类型
 *
 * @Author HK-LJJ
 * @Date 2019/12/6
 * @Description
 */
public class FileType {
    /**
     * 未知
     */
    public static final int UNKNOWN = -1;

    /**
     * 所有类型
     */
    public static final int ALL = -2;

    /**
     * 图片
     */
    public static final int IMAGE = 1;
    /**
     * 视频
     */
    public static final int VIDEO = 2;
    /**
     * 音频
     */
    public static final int AUDIO = 3;
    /**
     * doc/docx
     */
    public static final int WORD = 4;
    /**
     * excel
     */
    public static final int EXCEL = 5;
    /**
     * pdf
     */
    public static final int PDF = 6;
    /**
     * txt
     */
    public static final int TXT = 7;

    /**
     * 文件类型
     * @Author HK-LJJ
     * @Date 2020/1/3
     * @Description
     */
    @IntDef({UNKNOWN,ALL,IMAGE,VIDEO,AUDIO,WORD,EXCEL,PDF,TXT})
    @Retention(SOURCE)
    @Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD})
    public @interface Type {

    }


//    public static final String MIME_IMAGE = "image/*";
//    public static final String MIME_VIDEO = "video/*";
//    public static final String MIME_AUDIO = "audio/*";


    public static final String MIME_TYPE_IMAGE="image/*";
    public static final String MIME_TYPE_VIDEO="video/*";
    public static final String MIME_TYPE_AUDIO="audio/*";
    public static final String MIME_TYPE_TXT="text/plain";
    public static final String MIME_TYPE_DOC="application/msword";
    public static final String MIME_TYPE_PDF="application/pdf";
    public static final String MIME_TYPE_EXCEL="application/vnd.ms-excel";

    /**
     * 文件后缀和MIMEType对应表
     */
    private static final String[][] MIMEType = {
            {"jpg", MIME_TYPE_IMAGE},
            {"jpeg", MIME_TYPE_IMAGE},
            {"png", MIME_TYPE_IMAGE},
            {"bmp", MIME_TYPE_IMAGE},
            {"gif", MIME_TYPE_IMAGE},
            {"webp", MIME_TYPE_IMAGE},

            {"mp4", MIME_TYPE_VIDEO},
            {"3gp", MIME_TYPE_VIDEO},
            {"3gpp", MIME_TYPE_VIDEO},
            {"3gpp2", MIME_TYPE_VIDEO},
            {"avi", MIME_TYPE_VIDEO},
            {"mkv", MIME_TYPE_VIDEO},
            {"webm", MIME_TYPE_VIDEO},


            {"mp3", MIME_TYPE_AUDIO},
            {"aac", MIME_TYPE_AUDIO},
            {"ogg", MIME_TYPE_AUDIO},
            {"amr", MIME_TYPE_AUDIO},
            {"wav", MIME_TYPE_AUDIO},
            {"m4a", MIME_TYPE_AUDIO},

            {"txt", MIME_TYPE_TXT},
            {"doc", MIME_TYPE_DOC},
            {"docx", MIME_TYPE_DOC},
            {"pdf", MIME_TYPE_PDF},
            {"xls", MIME_TYPE_EXCEL},
            {"xlsx", MIME_TYPE_EXCEL},

    };

    public static String getMimeType(int fileType) {
        return switch (fileType) {
            case IMAGE -> MIME_TYPE_IMAGE;
            case VIDEO -> MIME_TYPE_VIDEO;
            case AUDIO -> MIME_TYPE_AUDIO;
            case PDF -> MIME_TYPE_PDF;
            case WORD -> MIME_TYPE_DOC;
            case EXCEL -> MIME_TYPE_EXCEL;
            case TXT -> MIME_TYPE_TXT;
            default -> null;
        };
    }

    /**
     * 获取文件的mimeType 用于设置intent.setDataAndType(uri,type) 中的type;
     *
     * @param path 文件路径或后缀
     * @return 中的type
     */
    public static String getMimeType(String path) {
        String mimeType = "*/*";
        if (path == null) {
            return mimeType;
        }

        String fileType = path;
        int index = path.lastIndexOf(".");
        if (index >= 0) {
            fileType = path.substring(index + 1);
        }
        for (int i = 0; i < MIMEType.length; i++) {
            if (fileType.equalsIgnoreCase(MIMEType[i][0])) {
                mimeType = MIMEType[i][1];
                break;
            }
        }
        return mimeType;
    }


    /**
     * 通过文件后缀获取文件类型
     *
     * @param path 包含后缀的路径
     * @return {@link #IMAGE,#VIDEO,#AUDIO,#WORD,#PDF,#TXT ,#UNKNOWN}
     */
    public static int getType(String path) {
        if (path == null) {
            return UNKNOWN;
        }
        String mimeType = getMimeType(path);
        if (MIME_TYPE_IMAGE.equalsIgnoreCase(mimeType)) {
            return IMAGE;
        } else if (MIME_TYPE_VIDEO.equalsIgnoreCase(mimeType)) {
            return VIDEO;
        } else if (MIME_TYPE_AUDIO.equalsIgnoreCase(mimeType)) {
            return AUDIO;
        } else if (MIME_TYPE_DOC.equalsIgnoreCase(mimeType)) {
            return WORD;
        }else if (MIME_TYPE_EXCEL.equalsIgnoreCase(mimeType)) {
            return EXCEL;
        } else if (MIME_TYPE_PDF.equalsIgnoreCase(mimeType)) {
            return PDF;
        } else if (MIME_TYPE_TXT.equalsIgnoreCase(mimeType)) {
            return TXT;
        } else {
            return UNKNOWN;
        }
    }


}
